package cz.matasek.dbLogger.gui;

import cz.matasek.dbLogger.database.DatabaseConnection;
import cz.matasek.dbLogger.database.DatabasePlatform;
import cz.matasek.dbLogger.database.Oracle;
import cz.matasek.dbLogger.database.PostgreSql;
import cz.matasek.dbLogger.model.Database;
import javax.swing.DefaultComboBoxModel;

/**
 * This panel contains a form for connecting to the database.
 *
 * @author Luboš Matásek
 */
public class ConnectionPanel extends javax.swing.JPanel {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = -6608407758514955833L;

    /** 
     * Creates a new </code>ConnectionPanel</code>.
     */
    public ConnectionPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _username = new javax.swing.JTextField();
        _password = new javax.swing.JPasswordField();
        _databaseName = new javax.swing.JTextField();
        _host = new javax.swing.JTextField();
        _port = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        _connect = new javax.swing.JButton();
        _databasePlatform = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(442, 309));

        _username.setText("postgres");

        _password.setText("postgres");
        _password.setPreferredSize(new java.awt.Dimension(59, 20));

        _databaseName.setText("bakalarka");

        _host.setText("localhost");

        _port.setText("5432");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Username");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Password");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Database name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Host");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Port");

        _connect.setText("Connect");
        _connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _connectActionPerformed(evt);
            }
        });

        _databasePlatform.setModel(new DefaultComboBoxModel(DatabasePlatform.values()));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Database platform");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel1)))
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_connect)
                    .addComponent(_databasePlatform, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_databaseName, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_host, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_port, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_username, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_password, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(_databaseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(_host, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_databasePlatform, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(_connect)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Performs connection to the database.
     *
     * @param evt action event
     */
    private void _connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__connectActionPerformed
        // creates a new database connection
        DatabaseConnection databaseConnection = null;
        switch ((DatabasePlatform) _databasePlatform.getSelectedItem()) {
            case ORACLE:
                databaseConnection = new Oracle(
                        _username.getText(), new String(_password.getPassword()),
                        _host.getText(), _databaseName.getText(), _port.getText());
                break;
            case POSTGRE_SQL:
                databaseConnection = new PostgreSql(
                        _username.getText(), new String(_password.getPassword()),
                        _host.getText(), _databaseName.getText(), _port.getText());
                break;
            default:
                assert false :
                        "Unsupported database platform submitted: " +
                        (DatabasePlatform) _databasePlatform.getSelectedItem();
        }


        // tries connect to the database
        try {
            databaseConnection.connect();

            // creates database model
            Database database = new Database(databaseConnection);

            // refreshes GUI
            Gui gui = Gui.getInstance();
            gui.setDatabase(database);
            gui.setConnected();
            gui.repaint();
        } catch (Exception ex) {
            // shows dialog that connection wasn't established
            Dialog.error(ex.getMessage());
        }
}//GEN-LAST:event__connectActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _connect;
    private javax.swing.JTextField _databaseName;
    private javax.swing.JComboBox _databasePlatform;
    private javax.swing.JTextField _host;
    private javax.swing.JPasswordField _password;
    private javax.swing.JTextField _port;
    private javax.swing.JTextField _username;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables
}
